package com.blog.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.commons.utils.DateUtils;
import com.blog.commons.web.domain.request.PageDomain;
import com.blog.commons.web.domain.response.PageResult;
import com.blog.dao.BlogCommentsDao;
import com.blog.mapper.BlogCommentMapper;
import com.blog.pojo.TbBlogComment;
import com.blog.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @program: dal-blog
 * @description: 评论
 * @author: jian
 * @create: 2021-08-08 13:48
 **/
@Service
public class CommentsServiceImpl implements CommentsService {

    @Resource
    private BlogCommentMapper commentsMapper;


    @Override
    public void save(TbBlogComment comment) {
        comment.setCommentCreateTime(new Date());
        comment.setCommentStatus(0);
        comment.setIsDeleted(0);
        commentsMapper.insert(comment);
    }

    @Override
    public PageResult<TbBlogComment> page(PageDomain domain, TbBlogComment comment) {
        IPage<TbBlogComment> page = new Page(domain.getPage(),domain.getLimit());
        QueryWrapper<TbBlogComment> wrapper = new QueryWrapper<>();
        IPage<TbBlogComment> iPage = commentsMapper.selectPage(page, wrapper);
        List<TbBlogComment> comments = iPage.getRecords();
        if (null != comments && comments.size()>0){
            comments.forEach(c -> {
                if (null !=c.getCommentCreateTime()){
                    try {
                        c.setCreateTime(DateUtils.parseDate2String(c.getCommentCreateTime(),"yyyy-MM-dd hh:mm:ss"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        return new PageResult<>(comments,iPage.getSize(),iPage.getTotal());
    }

    @Override
    public List<TbBlogComment> list(Integer blogId) {
        QueryWrapper<TbBlogComment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbBlogComment::getBlogId,blogId);
        List<TbBlogComment> comments = commentsMapper.selectList(wrapper);
        if (null != comments && comments.size()>0){
            comments.forEach(comment -> {
                if (null !=comment.getCommentCreateTime()){
                    try {
                        comment.setCreateTime(DateUtils.parseDate2String(comment.getCommentCreateTime(),"yyyy-MM-dd hh:mm:ss"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        List<TbBlogComment> commentList = dg(comments, 0);
        return commentList;
    }

    public List<TbBlogComment> dg(List<TbBlogComment> list,Integer parentId){
        List<TbBlogComment> comments = new ArrayList<>();
        Iterator<TbBlogComment> it = list.iterator();
        while (it.hasNext()){
            TbBlogComment comment = it.next();
            if (parentId.equals( comment.getParentId())) {
                comment.setCommentList(dg(list, comment.getCommentId()));
                comments.add(comment);
            }
        }
//        for (TbBlogComment menu : list) {
//            if (parentId.equals( menu.getParentId())) {
//                menu.setCommentList(dg(list, menu.getCommentId()));
//                list.add(menu);
//            }
//        }
        return comments;

    }
}
